﻿using ManagementApi.Models;
using Modules;
using Modules.Person;
using Modules.System.Area;

namespace ManagementApi;

/// <summary>
/// 人员证书字典扩展类
/// </summary>
public static partial class PersonCertificateExceptions
{
    /// <summary>
    /// 获取VUE树形结构
    /// </summary>
    /// <param name="certificateDists"></param>
    /// <returns></returns>
    public static IEnumerable<VueTree<string>> AsTrees(this IEnumerable<PersonCertificateDist> certificateDists)
    {
        return RecurChildTrees("0", certificateDists);
    }

    /// <summary>
    /// 递归子级
    /// </summary>
    /// <param name="pid"></param>
    /// <param name="certificateDists"></param>
    /// <returns></returns>
    private static IEnumerable<VueTree<string>> RecurChildTrees(string pid, IEnumerable<PersonCertificateDist> certificateDists)
    {
        List<VueTree<string>> entities = new List<VueTree<string>>();
        foreach (var item in certificateDists)
        {
            VueTree<string> tree = new();
            if (item.ParentId == pid)
            {
                tree.Id = item.Id;
                tree.Value = item.Id;
                tree.Label = item.Name;
                tree.Extend1 = item.Type;
                tree.Extend2 = item.Level;
                tree.Children = RecurChildTrees(item.Id, certificateDists);
                if (tree.Children.Count() == 0)
                {
                    tree.leaf = true;
                }
                entities.Add(tree);
            }
        }
        return entities;
    }
}
